package InterfaceUsuario;

import Exception.TrianguloException;
import Funcoes.Triangulo;
import java.awt.Toolkit;
import java.awt.Image;
import java.io.File;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * @author  Bruna Christina
 * @version 1.0
 */
public class TelaPrincipal extends javax.swing.JFrame {

    public TelaPrincipal() {
        initComponents();
    }
  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        tbxLadoA = new javax.swing.JTextField();
        tbxLadoB = new javax.swing.JTextField();
        tbxLadoC = new javax.swing.JTextField();
        lbLadoA = new javax.swing.JLabel();
        lbLadoB = new javax.swing.JLabel();
        lbLadoC = new javax.swing.JLabel();
        btnClassificar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Programa Triângulo");
        setMinimumSize(new java.awt.Dimension(300, 160));
        getContentPane().setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(tbxLadoA, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(tbxLadoB, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(tbxLadoC, gridBagConstraints);

        lbLadoA.setText("Lado A");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        getContentPane().add(lbLadoA, gridBagConstraints);

        lbLadoB.setText("Lado B");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(lbLadoB, gridBagConstraints);

        lbLadoC.setText("Lado C");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        getContentPane().add(lbLadoC, gridBagConstraints);

        btnClassificar.setText("Classificar");
        btnClassificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClassificarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(btnClassificar, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnClassificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClassificarActionPerformed
        try {
            if ((Double.parseDouble(tbxLadoA.getText()) <= 0) || (Double.parseDouble(tbxLadoB.getText()) <= 0) || (Double.parseDouble(tbxLadoC.getText()) <= 0)) {
                JOptionPane.showMessageDialog(this, "Campo(s) com valor negativo ou zero", "Erro!", JOptionPane.ERROR_MESSAGE, createImageIcon("Imagens/Error.png", "Mensagem de Erro"));
            } else {
                Triangulo triangulo = new Triangulo(Double.parseDouble(tbxLadoA.getText()), Double.parseDouble(tbxLadoB.getText()), Double.parseDouble(tbxLadoC.getText()));
                String tipoTriangulo = triangulo.getTipoTriangulo().toString();
                JOptionPane.showMessageDialog(this, tipoTriangulo, "A Classificação do Triângulo: ", JOptionPane.INFORMATION_MESSAGE, createImageIcon("Imagens/" + tipoTriangulo + ".png", "Tipo de Triangulo"));
            }
        } catch (NumberFormatException e) {
            String msgErro = "";
            if (tbxLadoA.getText().equals("")) {
                msgErro += "\nO campo Lado A é obrigatório!";
            } else if (tbxLadoB.getText().equals("")) {
                msgErro += "\nO campo Lado B é obrigatório!";
            } else if (tbxLadoC.getText().equals("")) {
                msgErro += "\nO campo Lado C é obrigatório!";
            }
            JOptionPane.showMessageDialog(this, "Campo(s) com valor invalido" + msgErro, "Erro!", JOptionPane.ERROR_MESSAGE, createImageIcon("Imagens/Error.png", "Mensagem de Erro"));
        } catch (TrianguloException e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE, createImageIcon("Imagens/Error.png", "Mensagem de Erro"));
        }
    }//GEN-LAST:event_btnClassificarActionPerformed

    private ImageIcon createImageIcon(String path, String description)
    {
        File imageFile = new File(path);
        Image icon = null;

        try
        {
            icon = Toolkit.getDefaultToolkit().getImage(imageFile.toURI().toURL());
        } 
        catch (MalformedURLException ex)
        {
            Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (icon != null) 
            return new ImageIcon(icon);
        else
        {
            System.err.println("A imagem não pode ser encontrada no diretorio: " + path);
            return null;
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClassificar;
    private javax.swing.JLabel lbLadoA;
    private javax.swing.JLabel lbLadoB;
    private javax.swing.JLabel lbLadoC;
    private javax.swing.JTextField tbxLadoA;
    private javax.swing.JTextField tbxLadoB;
    private javax.swing.JTextField tbxLadoC;
    // End of variables declaration//GEN-END:variables
}
